/*var express = require('express');
var app = express();

app.use(express.bodyParser());

app.get('/', function(req, res) {
	console.log('GET Received');
	console.dir(req.body);
	res.send('Node Responding');
})

app.listen(8887);*/

/*var http = require("http");
console.log("Server Started");
http.createServer(function(request, response) {
  response.writeHead(200, {"Content-Type": "text/plain"});
  response.write("Hello World");
  response.end();
  //response.send(JSON.stringify(request));
  console.log("Request Received");
  console.dir(request);
}).listen(8887);*/

var express = require("express");
var app = express();

var OID = 0;
var returnData = '{"OID": "1234567"}';

app.use(express.bodyParser());

//var server = express.createServer();
app.enable("jsonp callback", true);
app.get("/foo", function(req, res) {
    // parses the request url
    console.log('Get Received: ' + req.query.OID + ', ' + OID);
    //console.log('Callback: ' + req.query.callback);
    var callback = req.query.callback;
    //console.dir(req);
    //var theUrl = url.parse( req.url );

    // gets the query part of the URL and parses it creating an object
    //var queryObj = queryString.parse( theUrl.query );

    // queryObj will contain the data of the query as an object
    // and jsonData will be a property of it
    // so, using JSON.parse will parse the jsonData to create an object
    //var obj = JSON.parse( req.jsonData );
    //console.log(obj.OID);

    if(callback) {
		res.header('Content-Type', 'application/json');
  		res.header('Charset', 'utf-8') 
    	res.send(req.query.callback + '(' + JSON.stringify(returnData) + ')');
    }

    res.json("hello");
}).listen(8887);

app.post("/", function(req, res) {
	console.log('Post Received: ' + req.body.ID);
	returnData = '{"Offenders":[{"Offender":[{"OID":"' + req.body.ID + '"},{"Locations":[{"TimeStamp":"' + req.body.TimeStamp + '","Lat":"' + req.body.Latitude + '","Long":"' + req.body.Longitude + '"}]}]}]}'
	//console.log(returnData);
}).listen(8888);

//console.log(server.address().port());